﻿using CaterBll;
using CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaterUI
{
    public partial class FormMemberInfo : Form
    {
        public FormMemberInfo()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private int selectedRowIndex = -1;

        private void FormMemberInfo_Load(object sender, EventArgs e)
        {
            loadList();

            LoadTypeList();
        }

        MemberInfoBll miBll = new MemberInfoBll();
        private void loadList()
        {
            //使用键值对存储条件
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //收集用户名信息
            if(txtSname.Text!="")
            {
                dic.Add("mname", txtSname.Text);
            }
            //收集手机号
            if(txtSmobile.Text!="")
            {
                dic.Add("MPhone", txtSmobile.Text);
            }

            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = miBll.GetList(dic);

            //设置选中行
            if (selectedRowIndex > -1)
            {
                dgvList.Rows[selectedRowIndex].Selected = true;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtMName.Text == "")
            {
                MessageBox.Show("请填写类型名称！");
                return;
            }
            if (txtMPhone.Text == "")
            {
                MessageBox.Show("请填写电话！");
                return;
            }
            if (txtMMoney.Text == "")
            {
                MessageBox.Show("余额不能为空！");
                return;
            }
            if (Convert.ToDecimal(txtMMoney.Text.Trim()) <0)
            {
                MessageBox.Show("余额不能小于0！");
                return;
            }

            MemberInfo mi = new MemberInfo()
            {
                MName = txtMName.Text,
                MPhone = txtMPhone.Text,
                MMoney = Convert.ToDecimal(txtMMoney.Text.Trim()),
                MTypeId = Convert.ToInt32(cmbMtype.SelectedValue)
            };

            if (txtmid.Text.Equals("添加时无编号"))
            {
                

                if (miBll.Insert(mi) > 0)
                {
                    loadList();
                    clearText();
                }
            }
            else
            {
                mi.MId =Convert.ToInt32(txtmid.Text);
                if (miBll.Update(mi) > 0)
                {
                    loadList();
                    clearText();
                }
            }
        }


        private void LoadTypeList()
        {
            MemberTypeInfoBll mtiBll = new MemberTypeInfoBll();
            List<MemberTypeInfo> list = mtiBll.GetList();
            cmbMtype.DataSource = list;
            cmbMtype.DisplayMember = "mtitle";
            cmbMtype.ValueMember = "mid";
        }

        private void clearText()
        {
            txtmid.Text = "添加时无编号";
            txtMName.Text = "";
            txtMPhone.Text = "";
            txtMMoney.Text = "";
            btnSave.Text = "添加";
        }

        private void dgvList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedRowIndex = e.RowIndex;

            DataGridViewRow row = dgvList.Rows[e.RowIndex];
            txtmid.Text = row.Cells[0].Value.ToString();
            txtMName.Text = row.Cells[1].Value.ToString();
            cmbMtype.Text = row.Cells[2].Value.ToString();
            txtMPhone.Text = row.Cells[3].Value.ToString();
            txtMMoney.Text = row.Cells[4].Value.ToString();
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            clearText();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            //删除前确认
            DialogResult result = MessageBox.Show("你确定要删除吗？", "删除确认", MessageBoxButtons.OKCancel);
            if (result == DialogResult.Cancel)
            {
                return;
            }
            //获取选中的行
            var rows = dgvList.SelectedRows;
            if (rows.Count > 0)
            {
                int mid = int.Parse(rows[0].Cells[0].Value.ToString());
             

                if (miBll.Delete(mid) > 0)
                {
                    loadList();
                }
            }
            else
            {
                MessageBox.Show("请先选择要删除的行！");
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void txtSname_TextChanged(object sender, EventArgs e)
        {
            //文本改变事件
            loadList();
        }

        private void txtSmobile_Leave(object sender, EventArgs e)
        {
            //失去焦点事件
            loadList();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            txtSname.Text = "";
            txtSmobile.Text = "";
            loadList();
        }

        private void btnType_Click(object sender, EventArgs e)
        {
            DialogResult result = new DialogResult();
            //以模态窗口打开分类管理
            FormMemberTypeInfo formMemberTypeInfo = new FormMemberTypeInfo();
            //是否更新下拉列表
            result=formMemberTypeInfo.ShowDialog();
            if(result==DialogResult.OK)
            {
                LoadTypeList();
                loadList();
            }

        }
    }
}
